package com.itwlj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itwlj.dto.SpecDTO;
import com.itwlj.dto.SpecPageQueryDTO;
import com.itwlj.entity.Spec;
import com.itwlj.entity.SpecValue;
import com.itwlj.mapper.GoodsSpecLinksMapper;
import com.itwlj.mapper.SpecMapper;
import com.itwlj.mapper.SpecValueLinksMapper;
import com.itwlj.mapper.SpecValueMapper;
import com.itwlj.result.PageResult;
import com.itwlj.service.SpecService;
import com.itwlj.vo.SpecVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 规格业务层
 */
@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private SpecMapper specMapper;
    @Autowired
    private SpecValueLinksMapper specValueLinksMapper;
    @Autowired
    private SpecValueMapper specValueMapper;
    @Autowired
    private GoodsSpecLinksMapper goodsSpecLinksMapper;

    /**
     * 新增规格
     *
     * @param specDTO
     */
    @Transactional
    public void insert(SpecDTO specDTO) {
        Spec spec = new Spec();
        BeanUtils.copyProperties(specDTO, spec);
        specMapper.insert(spec);

        List<Long> specValueIds = specDTO.getSpecValueIds();
        for (Long specValueId : specValueIds) {
            specValueLinksMapper.insert(spec.getId(), specValueId);
        }
    }

    /**
     * 规格分页查询
     *
     * @param specPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SpecPageQueryDTO specPageQueryDTO) {
        PageHelper.startPage(specPageQueryDTO.getPage(), specPageQueryDTO.getPageSize());
        Page<Spec> page = specMapper.pageQuery(specPageQueryDTO);

        long total = page.getTotal();
        List<Spec> specList = page.getResult();

        List<SpecVO> records = new ArrayList<>();
        specList.forEach(spec -> {
            List<Long> specValueIds = specValueLinksMapper.getSpecValueIds(spec.getId());
            List<SpecValue> specValueList = specValueMapper.getByIds(specValueIds);

            SpecVO specVO = SpecVO.builder()
                    .id(spec.getId())
                    .name(spec.getName())
                    .specValueList(specValueList)
                    .build();
            records.add(specVO);
        });

        return new PageResult(total, records);
    }

    /**
     * 规格批量删除
     *
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        specMapper.deleteBatch(ids);
        specValueLinksMapper.deleteBySpecIds(ids);
    }

    /**
     * 查询规格
     *
     * @return
     */
    public List<SpecVO> list() {
        List<Spec> specList = specMapper.list();

        List<SpecVO> specVOList = new ArrayList<>();
        specList.forEach(spec -> {
            List<Long> specValueIds = specValueLinksMapper.getSpecValueIds(spec.getId());
            List<SpecValue> specValueList = specValueMapper.getByIds(specValueIds);

            SpecVO specVO = SpecVO.builder()
                    .id(spec.getId())
                    .name(spec.getName())
                    .specValueList(specValueList)
                    .build();
            specVOList.add(specVO);
        });
        return specVOList;
    }

    /**
     * 根据商品id查询规格
     *
     * @param goodsId
     * @return
     */
    public List<SpecVO> getByGoodsId(Long goodsId) {
        List<SpecVO> specVOList = new ArrayList<>();

        List<Long> SpecIds = goodsSpecLinksMapper.getSpecIds(goodsId);
        for (Long specId : SpecIds) {
            Spec spec = specMapper.getById(specId);
            List<Long> specValueIds = specValueLinksMapper.getSpecValueIds(specId);
            List<SpecValue> specValueList = specValueMapper.getByIds(specValueIds);

            SpecVO specVO = SpecVO.builder()
                    .id(spec.getId())
                    .name(spec.getName())
                    .specValueList(specValueList)
                    .build();
            specVOList.add(specVO);
        }

        return specVOList;
    }
}
